package com.lanou.renrenxin.tools;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.SoundPool;
import android.os.CountDownTimer;
import android.os.Environment;

import com.lanou.renrenxin.onclicklistener.LimitTimeListener;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by zouhui on 16/3/5.
 */
public class RecorderVoice {
    private  String filePath;
    private  int len;
    private File file;
    private  String paths;
    private MediaPlayer mediaPlayer;
    private  MediaRecorder mediaRecorder;
    private File file1;
    private  String path;
    private Long startTime;
    private Long stopTime;
    private LimitTimeListener limitTimeListener;
    private boolean isSend = false;
    private CountDownTimer countDownTimer;
    private SoundPool soundPool;
    private int music;

    public void setLimitTimeListener(LimitTimeListener limitTimeListener) {
        this.limitTimeListener = limitTimeListener;
    }

    public String getPaths() {
        return paths;
    }

    public void setPaths(String paths) {
        this.paths = paths;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public  void recorderVoice() {

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        // 设置输出格式
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        // 设置编码格式
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            try {
                path = Environment.getExternalStorageDirectory()
                        .getCanonicalPath().toString()
                        + "/renrenxinvoice";

                File files = new File(path);
                if (!files.exists()) {
                    files.mkdir();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        paths = path + "/" + new SimpleDateFormat("yyyyMMddHHmmss").format(System
                .currentTimeMillis()) + ".amr";
        setPaths(paths);
        file1 = new File(paths);
        mediaRecorder.setOutputFile(file1.getAbsolutePath());
        try {
            file1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        countDownTimer = new CountDownTimer(60000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                mediaRecorder.stop();
                stopTime = FormatTime.getSysTime();
                setLen((int) (stopTime - startTime)/1000);
                limitTimeListener.limitTime();

            }
        };
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            startTime = FormatTime.getSysTime();
            countDownTimer.start();

        } catch (IOException e) {
            e.printStackTrace();
        }



    }
    public  void stopRecorder(){
        mediaRecorder.stop();
        stopTime = FormatTime.getSysTime();
        setLen((int) (stopTime - startTime) / 1000);
        countDownTimer.cancel();
    }

    public void playVoice(String path){

        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean voiceIsSend(String path){
        return isSend;
    }
}
